package com.nc.shop.service.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nc.shop.service.dao.RepertoryDao;
import com.nc.shop.service.dao.ShopDao;
import com.nc.shop.service.model.entity.ClassifyEntity;
import com.nc.shop.service.model.entity.RepertoryEntity;
import com.nc.shop.service.model.entity.ShopEntity;
import com.nc.shop.service.service.RepertoryService;
import com.plug.result.ReqPage;
import com.plug.result.RetResponse;
import com.plug.result.RetResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Optional;

/**
 * 创建人:    xvzhe
 * 创建时间:
 * 修改时间:
 * 版本:      v1.0
 */
@Service
public class RepertoryServiceImpl implements RepertoryService {
    @Autowired
    RepertoryDao repertoryDao;
    @Autowired
    ShopDao shopDao;

    @Override
    public RetResult findPage(ReqPage reqPage) {
        JSONObject req = Optional.ofNullable(reqPage.getCommon()).orElseGet(JSONObject::new);
        RepertoryEntity entity = JSON.parseObject(req.toJSONString(), RepertoryEntity.class);
        int page = reqPage.getPage() - 1;
        int size = reqPage.getLimit();
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.ASC, "createTime");
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<RepertoryEntity> example = Example.of(entity, matcher);
        Page<RepertoryEntity> pages = repertoryDao.findAll(example, pageable);
        return RetResponse.makeRsp(0, "成功", pages.getContent(), (int) pages.getTotalElements());
    }

    @Override
    @Transactional
    public RetResult insert(RepertoryEntity repertoryEntity) {
        Optional<ShopEntity> opt = shopDao.findById(repertoryEntity.getShopEntity().getId());
        if (opt.isPresent()) {
            repertoryEntity.setShopEntity(opt.get());
            if (repertoryEntity.getNumber() <= 0) {
                return RetResponse.makeErrRsp("入库数量需大于0！");
            }
            repertoryDao.save(repertoryEntity);
            repertoryEntity.getShopEntity().setRepertory(repertoryEntity.getShopEntity().getRepertory() + repertoryEntity.getNumber());
            shopDao.save(repertoryEntity.getShopEntity());
            return RetResponse.makeOKRsp("入库成功");
        } else {
            return RetResponse.makeErrRsp("商品不存在");
        }
    }

    @Override
    @Transactional
    public RetResult out(RepertoryEntity repertoryEntity) {
        Optional<ShopEntity> opt = shopDao.findById(repertoryEntity.getShopEntity().getId());
        if (opt.isPresent()) {
            repertoryEntity.setShopEntity(opt.get());
            if (repertoryEntity.getNumber() <= 0) {
                return RetResponse.makeErrRsp("出库数量需大于0！");
            }
            if (opt.get().getRepertory() - repertoryEntity.getNumber() < 0) {
                return RetResponse.makeErrRsp("库存不足！");
            }
            repertoryDao.save(repertoryEntity);
            repertoryEntity.getShopEntity().setRepertory(repertoryEntity.getShopEntity().getRepertory() - repertoryEntity.getNumber());
            shopDao.save(repertoryEntity.getShopEntity());
            return RetResponse.makeOKRsp("出库成功");
        } else {
            return RetResponse.makeErrRsp("商品不存在");
        }
    }
}
